package com.hzcx.app.simplechat.ui.login;

import android.content.Intent;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.hzcx.app.simplechat.R;
import com.hzcx.app.simplechat.base.BaseActivity;
import com.hzcx.app.simplechat.ui.login.bean.LoginInfoBean;
import com.hzcx.app.simplechat.ui.login.contract.LoginByPwdContract;
import com.hzcx.app.simplechat.ui.login.presenter.LoginByPwdPresenter;
import com.hzcx.app.simplechat.ui.login.util.LoginUtil;
import com.hzcx.app.simplechat.ui.publicui.SendCodeActivity;
import com.hzcx.app.simplechat.ui.publicui.enums.SendCodeEnum;
import com.hzcx.app.simplechat.view.MainBlueConfirmButton;
import com.hzcx.app.simplechat.view.MainPwdEditText;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 登录界面-密码登录
 */
public class LoginByPwdActivity extends BaseActivity<LoginByPwdPresenter> implements LoginByPwdContract.View {

    public static final String INTENT_TAG_PHONE = "INTENT_TAG_PHONE";
    public static final String INTENT_TAG_PHONE_CITY = "INTENT_TAG_PHONE_CITY";

    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.iv_back)
    ImageView ivBack;
    @BindView(R.id.tv_tips)
    TextView tvTips;
    @BindView(R.id.et_pwd)
    MainPwdEditText etPwd;
    @BindView(R.id.iv_pwd_state)
    ImageView ivPwdState;
    @BindView(R.id.btn_login)
    MainBlueConfirmButton btnLogin;
    @BindView(R.id.tv_login_by_code)
    TextView tvLoginByCode;
    @BindView(R.id.tv_forget_pwd)
    TextView tvForgetPwd;

    private LoginInfoBean loginInfoBean;

    @Override
    public int getLayout() {
        return R.layout.activity_login_by_pwd;
    }

    @Override
    public void initPresenter() {
        mPresenter = new LoginByPwdPresenter();
    }

    @Override
    public void initView() {
        tvTitle.setText("输入密码");
        etPwd.setIvPwdState(ivPwdState);
        btnLogin.setEditText(etPwd, 6);
    }

    @Override
    public void initData() {

    }

    @OnClick({R.id.tv_login_by_code, R.id.tv_forget_pwd, R.id.btn_login})
    public void viewOnClick(View view) {
        switch (view.getId()) {
            case R.id.tv_login_by_code: // 验证码登录
                startActivity(new Intent(this, SendCodeActivity.class)
                        .putExtra(SendCodeActivity.INTENT_EUNM, SendCodeEnum.LOGIN)
                        .putExtra(SendCodeActivity.INTENT_PHONE_CITY, getIntent().getStringExtra(INTENT_TAG_PHONE_CITY))
                        .putExtra(SendCodeActivity.INTENT_PHONE, getIntent().getStringExtra(INTENT_TAG_PHONE)));
                break;

            case R.id.tv_forget_pwd: // 忘记密码
                startActivity(new Intent(this, SendCodeActivity.class)
                        .putExtra(SendCodeActivity.INTENT_EUNM, SendCodeEnum.FORGET_PWD)
                        .putExtra(SendCodeActivity.INTENT_PHONE_CITY, getIntent().getStringExtra(INTENT_TAG_PHONE_CITY))
                        .putExtra(SendCodeActivity.INTENT_PHONE, getIntent().getStringExtra(INTENT_TAG_PHONE)));
                break;

            case R.id.btn_login:// 密码登录
                mPresenter.loginByPwd(this, getIntent().getStringExtra(INTENT_TAG_PHONE), etPwd.getText().toString(),
                        getIntent().getStringExtra(INTENT_TAG_PHONE_CITY));
                break;
        }
    }

    /**
     * 密码登录
     *
     * @param data
     */
    @Override
    public void loginSuccess(LoginInfoBean data) {
        loginInfoBean = data;
        LoginUtil.loginSuccess(this, data);
    }
}
